package nz.ac.massey.jbuif.utils;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;


/**
 * class jar loader
 * @author ahkiouma
 *
 */
public class JarClassLoader extends URLClassLoader {
	public JarClassLoader(URL url) {
		super(new URL[] { url });
	}

	
	/**
	 * JarClassLoader
	 * @param urls file(s) url's
	 * @param oldCL ClassLoader instance
	 */
	public JarClassLoader(URL[] urls, ClassLoader oldCL) {
		super(urls, oldCL);
	}

	
	/**
	 * add a url
	 * @param url instance of URL
	 * @throws MalformedURLException
	 */
	public void addJar(URL url) throws MalformedURLException {
		if (url != null) {
			addURL(url);
		}
	}
	
	
	/**
	 * 
	 * @param f instance of a file
	 * @return the file url
	 * @throws MalformedURLException
	 */
	public static URL getURLForFile(File f) throws MalformedURLException
	{
		if(f.getName().toLowerCase().endsWith(".jar"))
		{
			return new URL("jar:" + f.toURI().toURL().toString() + "!/");
		}
		else
		{
			return f.toURI().toURL();
		}
	}
}
